package lvbu.wang.lvbuoperator;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.EditText;

import com.yzq.zxinglibrary.common.Constant;

import lvbu.wang.lvbuoperator.constants.ConstantsValue;
import lvbu.wang.lvbuoperator.core.BaseActivity;
import lvbu.wang.lvbuoperator.utils.PrintQrCode1Proxy;

public class PrintQR1Ac extends BaseActivity {

    private PrintQrCode1Proxy proxy;

    private EditText contentEt;
    private Button pasteBtn, scanBtn, printBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_print_qr);
    }

    @Override
    protected void init() {
        contentEt = findViewById(R.id.content_et);
        pasteBtn = findViewById(R.id.paste_btn);
        scanBtn = findViewById(R.id.scan_btn);
        printBtn = findViewById(R.id.print_btn);

        proxy = new PrintQrCode1Proxy(this);

        printBtn.setOnClickListener(view -> {
            String txt = contentEt.getText().toString().trim();
            if (!txt.isEmpty()) proxy.print(txt);
        });

        pasteBtn.setOnClickListener(view -> {
            ClipData d = ((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)).getPrimaryClip();
            contentEt.setText(d != null ? d.getItemAt(0).getText() : null);
        });

        scanBtn.setOnClickListener(view -> Quick.startQRCode(this, ConstantsValue.INTENT_SCAN_EWM1));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ConstantsValue.INTENT_SCAN_EWM1 && resultCode == RESULT_OK)
            contentEt.setText(data.getExtras() != null ? data.getExtras().getString(Constant.CODED_CONTENT) : null);
    }

    @Override
    protected void onDestroy() {
//        proxy.quit();
        super.onDestroy();
    }
}
